<?php
/**
 * 中奖记录
 */
class Winners extends CS_Controller
{
    public function _init()
    {
        $this->load->helper('common');
        $this->load->model('lottery_winner_model', 'lottery_winner');
        $this->load->model('region_model', 'region');
        $this->load->model('tourism_delivery_address_model', 'tourism_delivery_address');
        $this->load->model('tourism_provider_model','tourism_provider');
        $this->load->model('user_account_model', 'user_account');
        $this->load->model('activity_lottery_model', 'activity_lottery');
    }

    /**
     * 中奖记录
     */
    public function grid()
    {
        $this->data['region'] = $this->region->getChinaRegion(array('region_type' => 1));
        $this->data['adverts'] = $this->advertising();
        $lotteryId = get_cookie('lotteryunid');
        $this->data['winner'] = $this->lottery_winner->findByUId(array('lottery_id' => $lotteryId, 'uid' => $this->uid));
        $this->load->view('home/winners/grid', $this->data);
    }

    /**
     * 异步加载 中奖列表
     */
    public function ajaxWinners()
    {
        $lotteryId = get_cookie('lotteryunid');
        $data['winner'] = $this->lottery_winner->findByUId(array('lottery_id' => $lotteryId, 'uid' => $this->uid));
        echo json_encode(
                array(
                    'html' => $this->load->view('home/winners/ajaxWinners', $data, true)
                )
            );
        exit;
    }

    /**
     * 领取奖品提交
     */
    public function acceptPrizePost()
    {
        $postData = $this->input->post();
        if ( !isset($postData['id']) || $postData['id'] <= 0) {
            $this->jsonMessage('中奖者不存在');
        }
        $result = $this->lottery_winner->findById($postData['id']);
        if ($result->num_rows() == 0) {//中奖者不存在
            $this->jsonMessage('中奖者不存在');
        }
        $winner = $result->row();
        if ($winner->effective_date <= date('Y-m-d H:i:s') || $winner->status != 1 || $winner->prize_type != 1) { // 奖品已过期
            $this->jsonMessage('', '信息异常，尝试刷新');
        }

        $updateId = $this->lottery_winner->updateInfo($postData['id'], array('status' => 2, 'address' => $this->splitJointAddress($postData)));

        if ( $updateId) {
            $this->jsonMessage('');
        } else {
            $this->jsonMessage('信息更新失败');
        }
    }

    /**
     * json格式打包地址
     * @param $data
     * @return mixed|string|void
     */
    private function splitJointAddress($data)
    {
        $address = array();
        $address['receiver_name'] = $data['receiver_name'];
        $address['tel'] = $data['tel'];
        $address['detailed'] = $data['provinceName']. $data['cityName']. $data['districtName']. $data['detailed'];
        return json_encode($address );
    }

    /**
     * 领取虚拟奖品提交
     */
    public function acceptVirtualPrizePost()
    {
        $postData = $this->input->post();
        if ( !isset($postData['id']) || $postData['id'] <= 0) {
            $this->jsonMessage('中奖者不存在');
        }
        $result = $this->lottery_winner->findById($postData['id']);
        if ($result->num_rows() == 0) {//中奖者不存在
            $this->jsonMessage('中奖者不存在');
        }

        $winner = $result->row();
        if ($winner->effective_date <= date('Y-m-d H:i:s') || $winner->status != 1 || $winner->prize_type != 2) { // 奖品已过期
            $this->jsonMessage('', '信息异常，尝试刷新');
        }

        $updateId = $this->lottery_winner->updateInfo($postData['id'], array('status' => 2, 'address' => json_encode($postData)));
        if ( !$updateId) {
            $this->jsonMessage('信息更新失败');
        } else {
            $this->jsonMessage('');
        }
    }

    public function bzCurrency()
    {
        $winnerId = $this->input->get('id');
        $result = $this->lottery_winner->findById($winnerId);
        if ($result->num_rows() == 0) {
            $this->redirect('home/winners/grid');
        }

        $winner = $result->row();
        if ($winner->prize_type == 3 && $winner->status == 1) {
            $updateId = $this->lottery_winner->updateInfo($winnerId, array('status' => 3));
            if ($winner->amount > 0) {
                $this->user_account->updateUserAccount($this->uid, array('amount_bonus' => $winner->amount));//金币充值
            }
            $winner->status = 3;
        }

        $this->jsonMessage('');
    }

    /**
     * 中奖记录详情
     */
    public function detail($winnerId)
    {
        $result = $this->lottery_winner->findById($winnerId);
        if ($result->num_rows() == 0) {
            $this->redirect('home/winners/grid');
        }
        $winner = $result->row();

        if ($winner->status <= 1) {
            $this->redirect('home/winners/grid');
        }

        $lotteryId = get_cookie('lotteryunid');
        $lottery = $this->getCacheLottery($lotteryId);

        if ($lottery) {
            $data['page5'] = json_decode($lottery->page5);
        }

        $this->data['winner'] = $winner;
        $this->load->view('home/winners/detail', $this->data);
    }

    /**
     * 现场颁奖
     */
    public function ajaxAwards()
    {
        $winnerId = $this->input->get('id');
        if ($winnerId <= 0) {
            $this->jsonMessage('中奖者不存在');
        }
        $result = $this->lottery_winner->findById($winnerId);
        if ($result->num_rows() == 0) {
            $this->jsonMessage('中奖者不存在');
        }
        $winner = $result->row();
        if ($winner->effective_date <= date('Y-m-d H:i:s') || $winner->status != 1 || $winner->prize_type != 4) { // 奖品已过期
            $this->jsonMessage('', '信息异常，尝试刷新');
        }

        $updateId = $this->lottery_winner->updateInfo($winnerId, array('status' => 2));

        if ($updateId) {
            $this->jsonMessage('', '领取成功');
        } else {
            echo $this->db->last_query();exit;
            $this->jsonMessage('更新失败');
        }
        exit;
    }

    /**
     * 广告位
     */
    public function advertising()
    {
        $id = $this->input->get('id');
        $lotteryId = ($id) ? $id : get_cookie('lotteryunid');
        if (empty($lotteryId)) {
            $lotteryId = 13;//默认着想的活动
        }

        $lottery = $this->getCacheLottery($lotteryId);
        if ($lotteryId != 13) {
            if ($lottery) {
                $adverts = empty($lottery->page4) ? '' : json_decode($lottery->page4);
                if ( !empty($adverts->ad_image)) {
                    return $adverts;
                }
            }
            $lottery = $this->getCacheLottery(13);
        }
        if ($lottery === FALSE) {
            return false;
        }
        $adverts = empty($lottery->page4) ? '' : json_decode($lottery->page4);
        return $adverts;
    }

    /**
     * 获取活动缓存
     */
    private function getCacheLottery($lotteryId)
    {
        $activity = $this->cache->memcached->get('activityLottery'.$lotteryId); //缓存该活动
        if (empty($activity)) {
            $activity = $this->activity_lottery->findById($lotteryId);
            if ($activity->num_rows() == 0) {
                return FALSE;
            }
            $this->cache->memcached->save('activityLottery'.$lotteryId, $activity->row(), 24 * 60 * 60);
            $activity = $activity->row();
        }
        return $activity;
    }
}
